from dataclasses import dataclass
from typing import TypeVar, Optional, Generic

from pydantic import Field, BaseModel


# 定义状态类型
@dataclass
class Status:
    code: str = 'E99999'
    message: str = '服务器内部错误，请联系管理员'
    http_code: int = 515
    success: bool = False


success_status = Status(code='S00000', success=True, message='success', http_code=200)
common_error_status = Status()


class BaseResponse(BaseModel):
    code: str = Field(default=success_status.code)
    success: bool = Field(default=success_status.success)
    message: str = Field(default=success_status.message)
    data: dict = Field(default={})


ResponseDataType = TypeVar("ResponseDataType")


class GenericBaseResponse(BaseResponse, Generic[ResponseDataType]):
    data: Optional[ResponseDataType]


# ### response 数据格式定义 ###
class PaginationData(BaseModel):
    index: int = Field(default=1, description='查看第几页', title='当前页码')
    limit: int = Field(default=10, description='每页显示几个数据', title='每页数据')
    offset: int = Field(default=0, description='从第几个数据开始读取，也就是index和limit的乘积', title='偏移量')
    total: int = Field(default=0, description='数据总量', title='总数', examples=[300])


class GetAllData(BaseModel):
    items: list = Field(default=[], description="列表数据", title="数据")
    pagination: PaginationData
